package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCardDetailContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.FamilyCard;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletCardDetailPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxTimerUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.SystemUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.util.ZXingUtils;
import com.meitianhui.convenitenbuy.widget.dialog.SettingStoreDialog;
import com.meitianhui.convenitenbuy.widget.dialog.ShareDialog;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import javax.inject.Inject;

public class WalletCardDetailActivity extends BaseToolbarActivity implements WalletCardDetailContract.View {
    private FamilyCard mBean;
    private ImageView mIvCode;
    private EditText mEtRemark;
    private RxTimerUtils mRx;
    private SettingStoreDialog mDialog;
    @Inject
    WalletCardDetailPresenter mPresenter;
    private UMImage mUmImage;
    private ShareDialog mShareDialog;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle(getString(R.string.wallet_family_card));
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);

        mBean = getIntent().getParcelableExtra(Constants.ARGUMENT);
        initId();
        initInfo();
        isEnable(false);
    }

    private void initInfo() {
        if (mBean != null) {
            mEtRemark.setText(mBean.getRemark());
            try {
                Bitmap qrCode = ZXingUtils.createQRCode(mBean.getCard_no(), 450);
                mIvCode.setImageBitmap(qrCode);
                Animation a = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
                mIvCode.setAnimation(a);
            } catch (WriterException e) {
                e.printStackTrace();
            }
        }

    }

    private void initId() {
        mRx = new RxTimerUtils();
        mIvCode = getViewById(R.id.iv_wallet_card_detail_code);
        mEtRemark = getViewById(R.id.et_wallet_card_detail_remark);
        mEtRemark.setCursorVisible(false);
        RxUtils.bindEvents(getViewById(R.id.btn_wallet_card_Detail_delete), this);
        RxUtils.bindEvents(getViewById(R.id.btn_wallet_card_detail_record), this);
        RxUtils.bindEvents(getViewById(R.id.btn_wallet_card_detail_share), this);
        RxTimerUtils.IRxNext next = new RxTimerUtils.IRxNext() {
            @Override
            public void doNext(long number) {
                SystemUtils.hidKeyBoard(WalletCardDetailActivity.this);
                mEtRemark.setCursorVisible(false);
                if (!TextUtils.equals(mEtRemark.getText().toString().trim(), mBean.getRemark())) {
                    mPresenter.modifyRemark(mEtRemark.getText().toString().trim(), mBean.getCard_no());
                }
            }
        };
        mEtRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mRx.cancel();
                mRx.timer(3000, next);
            }
        });
        mEtRemark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtRemark.setCursorVisible(true);
                mEtRemark.setSelection(mEtRemark.getText().toString().length());
            }
        });
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_wallet_card_detail_share:
                if (mUmImage == null)
                    mUmImage = new UMImage(this, R.mipmap.logo);
                ShareDialog.OnShareClickListener mShareListener = mPresenter.shareListener(mUmImage, mBean.getCard_no(),shareListener);

                mShareDialog = new ShareDialog(this, mShareListener,  Constants.TYPE_NO_SHOW_WXMINI_SHARE);
                mShareDialog.show();
                TextView wechat = (TextView) mShareDialog.findViewById(R.id.btn_share_friendcircle);
                TextView qq = (TextView) mShareDialog.findViewById(R.id.btn_share_qq_zone);
                wechat.setVisibility(View.GONE);
                qq.setVisibility(View.GONE);
                wechat.setClickable(false);
                qq.setClickable(false);
                break;
            case R.id.btn_wallet_card_detail_record:
                Intent intent = new Intent(this, WalletCardRecordActivity.class);
                intent.putExtra(Constants.ARGUMENT, mBean.getCard_no());
                startActivity(intent);
                break;
            case R.id.btn_wallet_card_Detail_delete:
                clickDelete();
                break;
        }
    }

    private void clickDelete() {
        if (mDialog == null) {
            mDialog = new SettingStoreDialog(this, R.style.dialog_with_alpha, new SettingStoreDialog.AlertDialogListener() {
                @Override
                public void onConfirmClick() {
                    unBindCard();
                }

                @Override
                public void onCancelClick() {
                    mDialog.dismiss();

                }
            });
            mDialog.show();
            mDialog.showCancelBtn(true);
            mDialog.setDialogTitle(R.string.wallet_card_detail_delete_confirm);
            mDialog.setDialogContent(R.string.wallet_card_detail_delete_tip);
            mDialog.setCancelBtnText(getString(R.string.dialog_cancel));
            mDialog.setConfirmBtnText(getString(R.string.dialog_confirm));
        }
    }

    private void unBindCard() {
        mPresenter.unBindCard(mBean.getCard_no());
        mDialog.dismiss();
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_card_detail;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void unBindCardBack(boolean result, String msg) {
        if (result) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_card_detail_delete_success), 1);
            RxBus.get().post(new EventBean(Constants.EVENT_TYPE_FAMILY_CARD_UPDATE));
            finish();
        } else {
            TipUtils.showToast(App.getInstance(), msg);
        }
    }

    @Override
    public void modifyRemarkBack(boolean result, String msg) {
        if (result) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_card_detail_modify_success), 1);
            RxBus.get().post(new EventBean(Constants.EVENT_TYPE_FAMILY_CARD_UPDATE));
        } else {
            TipUtils.showToast(App.getInstance(), msg);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRx.cancel();
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            TipUtils.showToast(WalletCardDetailActivity.this,"成功了");
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(WalletCardDetailActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(WalletCardDetailActivity.this, "取消了", Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public void dismissDialog() {
        mShareDialog.dismiss();
    }
}
